import time
from selenium.webdriver.common.by import By
from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from LoginTest import Login



class PersonalsetingPage:
    url=""
    driver=""
    def __init__(self):
        self.url="http://49.235.61.184:8080/system/setting"
        self.driver=blog_driver.driver
        self.form = FormHelper(self.driver)
        # 公共自动登录方法，未登录时执行Login().LoginSuTest
        #self.form.auto_login(self.url, login_func=lambda: Login().LoginSuTest())
        self.driver.get(self.url)

    #检测页面元素
    def pageElements(self):
        elememts_check = {
        "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.title___3cGwa",
        "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.right___3-EXe > div.avatar___1GisI",
        "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.right___3-EXe > span > div > span > div > button",
        "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div:nth-child(2) > div > div.ant-col.ant-form-item-label > label"
        }
        blog_driver.GetScreeShot()
        if self.form.are_all_elements_present(elememts_check, 5):
            print("元素都存在")
        else:
            print("元素存在有问题")

    #提交--全填
    def CommitSucTest(self):
        time.sleep(5)
        self.form.fill_fields({"#name":"admin"})
        self.form.fill_fields({"#phone":"111"})
        self.form.fill_fields({"#email":"111"})
        self.form.fill_fields({"#profile":"111"})
        blog_driver.GetScreeShot()

        #提交
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")
        time.sleep(1)
        #检测
        self.form.verify_error_message("#sk-layout > div > div > section > div.ant-pro-layout-container > header.ant-layout-header.ant-pro-layout-header.ant-pro-layout-header-fixed-header.ant-pro-layout-header-mix.ant-pro-layout-header-fixed-header-action.ant-pro-layout-header-header > div > div.ant-pro-global-header-right-content > div > div > div > span > div > span:nth-child(2)","admin")

        # 清理

    def clear(self):
        self.form.clear_fields(["#name", "#phone", "#email", "#profile"])

    def commit(self,name,phone,email,profile):
        self.form.fill_fields({
            "#name": name,
            "#phone": phone,
            "#email": email,
            "#profile": profile,
        })

    def CommitFailTest(self, name="", username="", password="", repassword="", error_rules=None):
        # 清空
        self.clear()
        # 填充
        self.commit(name, username, password, repassword)
        # 提交
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div > div > div.right___2jdGg > div.baseView___3sRG3 > div.left___GavW0 > form > div.ant-space.ant-space-horizontal.ant-space-align-center > div:nth-child(2) > button")
        blog_driver.GetScreeShot()

        # 验证所有错误提示
        if error_rules:
            for css_selector, expected_text in error_rules.items():
                self.form.verify_error_message(css_selector, expected_text)

        blog_driver.GetScreeShot()


    #全不填
    def FailTest1(self):

        self.CommitFailTest("add","","","",
                            error_rules={
                                "#phone_help > div":"请输入您的联系电话!",
                                "#email_help > div":"请输入您的邮箱!",
                                "#profile_help > div":"请输入个人简介!"
                            })



# personal=PersonalsetingPage()
# personal.FailTest1()
